package com.ms.oschina.viewholder;

import android.graphics.Color;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.ms.oschina.R;
import com.ms.oschina.base.BaseViewHolder;
import com.ms.oschina.bean.Active;
import com.ms.oschina.bean.Active.ObjectReply;
import com.ms.oschina.fragment.MessageFragment;
import com.ms.oschina.util.RichTextHelper;
import com.ms.oschina.util.RichTextHelper.TextClickableSpan;
import com.ms.oschina.util.StringUtils;
import com.ms.oschina.util.UiUtils;
import com.ms.oschina.view.CircleImageView;
import com.ms.oschina.view.TextViewFixTouchConsume;
import com.squareup.picasso.Picasso;

public class AboutMeViewHolder extends BaseViewHolder<Active> {
	private int mark;

	private CircleImageView civAboutmeUserAvatar;
	private TextView tvAboutmeUserName;
	private TextView tvAboutmeTime;
	private TextView tvAboutmeDynamic;
	private TextViewFixTouchConsume tvAboutmeCommentContent;
	private TextView tvAboutmeMySeedInfo;

	private FrameLayout llAboutmeMySeedInfo;

	private void findViews(View view) {
		civAboutmeUserAvatar = (CircleImageView) view
				.findViewById(R.id.civ_aboutme_userAvatar);
		tvAboutmeUserName = (TextView) view
				.findViewById(R.id.iv_event_title);
		tvAboutmeTime = (TextView) view.findViewById(R.id.tv_aboutme_time);
		tvAboutmeDynamic = (TextView) view
				.findViewById(R.id.iv_event_time);
		tvAboutmeCommentContent = (TextViewFixTouchConsume) view
				.findViewById(R.id.tv_aboutme_commentContent);

		tvAboutmeMySeedInfo = (TextView) view
				.findViewById(R.id.tv_aboutme_mySeedInfo);
		llAboutmeMySeedInfo = (FrameLayout) view
				.findViewById(R.id.ll_aboutme_mySeedInfo);
	}

	@Override
	public View initView() {
		View view = UiUtils.inflate(R.layout.item_aboutme);
		findViews(view);
		return view;
	}

	@Override
	public void setData(int position, Active data) {
		switch (mark) {
		case MessageFragment.ATME:// @我
			llAboutmeMySeedInfo.setVisibility(8);
			tvAboutmeDynamic.setText("更新了动态");
			break;
		case MessageFragment.COMMENT:// 评论
			tvAboutmeDynamic.setText("回复了动态");
			llAboutmeMySeedInfo.setVisibility(0);

			ObjectReply objReply = data.getObjectReply();
			String objectName = objReply.getObjectName();
			String objectBody = objReply.getObjectBody();
			objectBody = objectBody.replace("\n", "");
			ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
			RichTextHelper.addSpan(tvAboutmeMySeedInfo, objectName, span,
					new TextClickableSpan(objectName));
			tvAboutmeMySeedInfo.append(" : " + objectBody);
			break;
		}

		tvAboutmeTime.setText(StringUtils.friendly_time(data.getPubDate()));
		tvAboutmeUserName.setText(data.getAuthor());
		tvAboutmeCommentContent.setText(data.getMessage());
		tvAboutmeUserName.setText(data.getAuthor());
		tvAboutmeUserName.setText(data.getAuthor());

		RichTextHelper.setTextRecombinationContent(tvAboutmeCommentContent,
				data.getMessage());

		String portrait = data.getPortrait();
		if (!TextUtils.isEmpty(portrait)) {
			Picasso.with(UiUtils.getContext()).load(portrait)
					.placeholder(R.drawable.widget_dface)
					.into(civAboutmeUserAvatar);
		}
	}

	public void setMark(int mark) {
		this.mark = mark;
	}

}
